load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_code.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/gsn_csm.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/contributed.ncl"
load "$NCARG_ROOT/lib/ncarg/nclscripts/csm/shea_util.ncl"

begin 

   datelist = asciiread("list_date",-1,"string") 
   mmlist = asciiread("list_mm",-1,"string") 

   nd = dimsizes(datelist) 

   chh = (/ "00", "06", "12", "18" /) 

   year = "2009" 

   ;; /glade/p/rda/data/ds627.0/ei.oper.an.ml/201101/ei.oper.an.ml.regn128sc.2011010100

   do id = 0, nd-1 

      rpath = "/glade/p/rda/data/ds627.0/ei.oper.an.ml/"+year+mmlist(id)+"/"

      do ihh = 0, 3 

      fnat = rpath + "ei.oper.an.ml.regn128sc."+year+datelist(id)+chh(ihh)
      fnbt = rpath + "ei.oper.an.ml.regn128uv."+year+datelist(id)+chh(ihh) 

      fna = "../tmp/ei.oper.an.ml.regn128sc."+year+datelist(id)+chh(ihh)+".grb"
      fnb = "../tmp/ei.oper.an.ml.regn128uv."+year+datelist(id)+chh(ihh)+".grb"

      ;;system("ln -s "+fnat+" "+fna) 
      ;;system("ln -s "+fnbt+" "+fnb) 

      fnc = "../input/cam5.nc" 

      print("read file "+fna) 
      print("read file "+fnb) 
 
      fla = addfile(fna,"r")
      flb = addfile(fnb,"r")
      flc = addfile(fnc,"r")

      ;; names  = getfilevarnames(fla)   ; extract ALL variable names

      fno = "../output/data_2x2.5_"+year+datelist(id)+chh(ihh)+".nc" 

      print("output filename : "+fno) 

      system("rm "+fno) 

      flo = addfile(fno,"c")   

      ;***************************************************
      ; loop over selected variables and rename on output
      ;***************************************************

      hyam_era= fla->lv_HYBL2_a
      hybm_era= fla->lv_HYBL2_b
      T = fla->T_GDS4_HYBL
      lnps= fla->LNSP_GDS4_HYBL
      Q = fla->Q_GDS4_HYBL
      U = flb->U_GDS4_HYBL
      V = flb->V_GDS4_HYBL
      lon= fla->g4_lon_1
      lat= fla->g4_lat_0
      lat= lat(::-1) 

      Q = (/ Q(:,::-1,:) /) 
      U = (/ U(:,::-1,:) /) 
      V = (/ V(:,::-1,:) /) 
      T = (/ T(:,::-1,:) /) 
      lnps = (/ lnps(::-1,:) /) 

      hyam_cam5= flc->hyam
      hybm_cam5= flc->hybm
      lon_cam5= flc->lon
      lat_cam5= flc->lat

      ps = lnps
      ps = (/ exp(lnps) /)  

      ps_2x2  = area_conserve_remap_Wrap (lon,lat,ps,lon_cam5,lat_cam5,False)
      T_2x2 = area_conserve_remap_Wrap (lon,lat,T,lon_cam5,lat_cam5,False)
      U_2x2 = area_conserve_remap_Wrap (lon,lat,U,lon_cam5,lat_cam5,False)
      V_2x2 = area_conserve_remap_Wrap (lon,lat,V,lon_cam5,lat_cam5,False)

      p0=1.e5

      T_2x2_L30 = hyi2hyo_Wrap(p0,hyam_era,hybm_era,ps_2x2,T_2x2,hyam_cam5,hybm_cam5,1)
      U_2x2_L30 = hyi2hyo_Wrap(p0,hyam_era,hybm_era,ps_2x2,U_2x2,hyam_cam5,hybm_cam5,1)
      V_2x2_L30 = hyi2hyo_Wrap(p0,hyam_era,hybm_era,ps_2x2,V_2x2,hyam_cam5,hybm_cam5,1)

      T_2x2_L30!0 = "lev" 
      U_2x2_L30!0 = "lev" 
      V_2x2_L30!0 = "lev" 
         
      flo->PS=ps_2x2 
      flo->T=T_2x2_L30
      flo->U=U_2x2_L30
      flo->V=V_2x2_L30
    
      delete(T_2x2_L30) 
      delete(U_2x2_L30) 
      delete(V_2x2_L30) 
      delete(ps_2x2) 
 
   end do 

   end do 


end 


